(function(){
	window.location.hash = "";
	window.location.hash = "#/index_section";
	var $container = $("#section_container") ,hashArr=["#index"] ,$index_section = $("#index_section");
	$.fn.CutScreen = function(object1){
		var _that_ = $(this);
		var	options = $.extend(true, {
				template:false,
				templateUrl:$(this).data("link"),
				trigger:new Function(),
				isHead:true,
				isBack:true,
				effect:"left",//up,down,left,right
				init : new Function()
			}, object1),
			effect = {
				"In":"anim active slideDownIn",
				"Out":" anim slideDownOut"
			};
		switch (options.effect){
			case "left":
				effect = {
					"In":"anim slideLeftIn",
					"In2":"anim slideRightIn",
					"Out":" anim slideLeftOut",
					"Out2":" anim slideRightOut"
				}
				break;
			default:
				break;
		};
		
		this.back = function(_this){
			var loc = window.location.hash;
			var pTar = _this.parents("section"),
				pTprev = pTar.prev("section");
				!!pTprev.size() ? pTprev : pTprev =  $("#index_section");
				pTar.removeClass(effect.In).addClass(effect.Out2).on("webkitAnimationEnd animationend",function(){
					pTprev.removeClass(effect.In2);
					pTar.remove();
				});
				pTprev.addClass(effect.In2);
			if(loc != ''){
				window.history.back()
			};
		};
		this.close = function(_this, callback){
			var pTar = _this.parents("section"),
					pTprev = $("#index_section");

			if (pTprev.size()>=1){
				pTar.prev("section").not("#index_section").remove();
			}
			pTar.removeClass(effect.In).addClass(effect.Out2).on("webkitAnimationEnd animationend",function(){
				pTprev.removeClass(effect.In2);
				pTar.remove();
			});
			pTprev.addClass(effect.In2);

			if (typeof (callback) === "function"){
				callback();
			}
		};
		var _this =this;
		$(this).off("click").on("click",function(){
			var	target = $(this).attr("data-target");
			var $dom = $("<section>",{class:"section_page",id:target}),
					$head =$("<header>"),
					$back = $("<a>",{"class":"page_back","href":"javascript:;",text:"<"});
					$back.one("click",function(e){
						_this.back($(this),$dom);
					});
					if(options.isHead){
						$head.html('<h1>标题</h1><nav></nav>').prepend($back);	
						$dom.append($head);
					}else{
						$dom.append($back);
					};
					$container.append($dom);
			if(!options.template){
				$.ajax({
					type:"get",
					url:options.templateUrl,
					async:true
				}).done(function(e){
					$dom.append(e);
					var prev = $dom.prev("section");
					!!prev.size() ? "" : prev = $index_section ;
					prev.addClass(effect.Out).one("animationend webkitAnimationEnd",function(){
						prev.removeClass(effect.Out);
					});
					$dom.addClass("active").show().addClass(effect.In).one("animationend webkitAnimationEnd",function(){
						$dom.removeClass(effect.In);
					});
					options.init.call(_this,$dom,$(this));
				});
			}else{
				$index_section = $("#index_section");
				var prev = $dom.prev("section");
				!!prev.size() ? "" : prev = $index_section ;
//							if(!!prev.size()){
					prev.addClass(effect.Out).one("animationend webkitAnimationEnd",function(){
						prev.removeClass(effect.Out);
					});
//							}
				$dom.addClass("active").show().addClass(effect.In).one("animationend webkitAnimationEnd",function(){
					$dom.removeClass(effect.In);
				});
				options.init.call(_this,$dom,$(this));
			};
			window.location.hash = "#/"+target;
		})
		try{
//			var oldUrl=document.location.hash,newURL;
//			window.onpopstate = function(event) {
//				var newURL = document.location.hash ,num =0;
//				$.each(hashArr, function(v,k) {
//					if(k == newURL){
//						console.log($container.children("section").find(".page_back"))
//						$container.children("section").last().find(".page_back").trigger("click")
//						++num;
//						hashArr.splice(v+1,v+2);
//						return false;
//					}
//				});
//				num == 0 ? hashArr.push(newURL) : "";
//			};
		}catch(e){
			console.log(e)
		}
		
	};
	
})();